package edu.hqu.deepspace.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.hqu.deepspace.pojo.Product;
import edu.hqu.deepspace.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/Product")
public class ProductController {
    @Autowired
    @Qualifier("ProductServiceImpl")
    private ProductService productService;

    @RequestMapping("/allProduct")
    public String list(@RequestParam(defaultValue="1") Integer currentPage, Model model, HttpServletRequest request, HttpSession session) {
        if(session.getAttribute("msg") != null)
            //判断全局变量是否存在
        {
            PageHelper.startPage(currentPage,4);
            List<Product> list = productService.queryAllProduct();
            PageInfo<Product> pageInfo=new PageInfo<Product>(list,9);
            System.out.println(pageInfo.getList());
            model.addAttribute("list", list);
            model.addAttribute("pageInfo", pageInfo);
            return "ProductManage";
        }
            return"Login";
    }
    //总商品界面


    @RequestMapping("/toAddProduct")
    public String toAddPaper(HttpSession session) {
        if(session.getAttribute("msg") != null){
            return "addProduct";
        }
        return "Login";
    }
    //跳转新增商品界面

    @RequestMapping("/addProduct")
    public String addPaper(Product product,HttpSession session) {
        if(session.getAttribute("msg") != null){
        System.out.println(product);
        productService.addProduct(product);
        return "redirect:/Product/allProduct";}
        return "Login";
    }

    //新增商品界面


    @RequestMapping("/toUpdateProduct")
    public String toUpdateProduct(Model model, int id,HttpSession session) {
        if(session.getAttribute("msg") != null){
        Product product = productService.queryProductById(id);
        System.out.println(product);
        model.addAttribute("product",product );
        return "updateProduct";}
        return "Login";
    }

    //跳转更新商品界面

    @RequestMapping("/updateProduct")
    public String updateBook(Model model, Product product,HttpSession session) {
        if(session.getAttribute("msg") != null){
        System.out.println(product);
        productService.updateProduct(product);
        Product products = productService.queryProductById(product.getPid());
        model.addAttribute("product", products);
        return "redirect:/Product/allProduct";}
        return "Login";
    }


    //更新商品界面


    @RequestMapping("/del/{pid}")
    public String deleteBook(@PathVariable("pid") int id,HttpSession session) {
        
        if(session.getAttribute("msg") != null){
        productService.deleteProductById(id);
        return "redirect:/Product/allProduct";}
        return "Login";
    }

    //删除

    @RequestMapping("/query")
    public String queryProduct(Model model,String pid,HttpSession session){
        if (session.getAttribute("msg") != null){
        int id=Integer.parseInt(pid);
        Product product = productService.queryProductById(id);
        model.addAttribute("product", product);
        return "queryProduct";}
        return "Login";
    }

    //查询商品
}
